<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\traits\Curd;
use app\api\controller\ApiController;
use app\api\model\SupplyOrder;
use app\api\service\ApiResponse;
use app\api\service\StatusCode;
use app\api\validate\v1\Orders as V1Orders;

class Orders extends ApiController
{
    protected $middleware = ['Token'];
    protected $modelName = 'SupplyOrder';
    protected $validate = 'Order';   //线索表验证器
   
    use Curd;

    /**
     * 首页列表
     * @MethodAuthor: 聂哥
     * @Date: 2023-03-11 02:12:55
     */
    public function listByIndex($num=5)
    {
        $allowFieds = ['intro','user_id','create_time','comments','likes'];
        $supply = SupplyOrder::with('user')
                    ->where(['type'=>1,'status'=>1])
                    ->field($allowFieds)
                    ->limit($num)
                    ->select();
        $need = SupplyOrder::with('user')
                    ->where(['type'=>2,'status'=>1])
                    ->field($allowFieds)
                    ->limit($num)
                    ->select();
        return ['statusCode'=>200,'data'=>['supply'=>$supply,'need'=>$need]];
    }
    public function add()
    {
        if($this->request->isPost())
        {
            $post = $this->request->post();
            

            $validate = new V1Orders();
            if (!$validate->check($post)){
                return ApiResponse::error(StatusCode::PARAM_ERROR,$validate->getError());                
            }
            $result = new SupplyOrder;
            if( !$result->save($post)){
                return ApiResponse::error(StatusCode::SERVER_ERROR,'保存失败');
            }
            return ApiResponse::success('保存成功');
        }
    }

}
